/**
 * Created by yunpeng on 2015/6/30.
 */
//var mongoose=require('mongoose');
//var config=require('../config');
//var mongodb = require('mongodb');
var mongoose=require('./mongoConn');
var Schema=mongoose.Schema;
//var mongodb_database=mongoose.createConnection(config.mongodb_url);
var _ = require("underscore");
/*mongodb_database.on('error',function(error){
    mongodb_database.close();
    console.log(error);
});

mongodb_database.on('close', function () {
    mongodb_database.open(config.mongodb_url);
    console.log("重新连接mongo");
});*/

var activeSchema = new Schema({
    box_id: String,
    imgurl:String,
    activename:String,
    content:String,
    status:Number,//0活动进行，1活动终止
    date:Date
});
//var Active = mongodb_database.model("active", activeSchema);
var Active = mongoose.model("active", activeSchema);

Active.prototype.saveActive=function(callback){
    this.save(function(err){
        callback(err);
    });
};


Active.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=Active.find(q,col).skip(obj.page.skipFrom).limit(obj.page.limit);

    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            Active.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null,count,pageCount, datas);
                }
            });
        }
    });
}

Active.prototype.removeActiveById=function (id,callback){
    Active.remove({_id:id}, function(err){
        return callback(err);
    });
}

//var Active = mongodb_database.model("active", activeSchema);
module.exports = Active;